package cn.kgc.easybuy.controller;

import cn.kgc.easybuy.annotaion.CheckPermission;
import cn.kgc.easybuy.pojo.Order;
import cn.kgc.easybuy.service.OrderService;
import cn.kgc.easybuy.utils.AjaxResult;
import cn.kgc.easybuy.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @CheckPermission({"0","1"})
    @GetMapping("/delOrder")
    public  AjaxResult delOrder(Integer id){
        if (id == null){
            throw new RuntimeException("删除订单id为空");
        }
        boolean flag = orderService.delOrder(id);
        if (flag) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
    @CheckPermission({"0","1"})
    @GetMapping("/cancelOrder")
    public AjaxResult cancelOrder(Integer id){
        if (id == null){
            throw new RuntimeException("取消订单id为空");
        }
        boolean flag = orderService.cancelOrder(id);
        if (flag){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @CheckPermission({"0","1"})
    @PostMapping("/updateOrder")
    public AjaxResult updateOrder(@RequestBody Order order){
        if (order == null){
            throw new RuntimeException("修改订单为空");
        }
        boolean flag = orderService.updateOrder(order);
        if (flag){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @CheckPermission({"0","1"})
    @PostMapping("/addOrder")
    public AjaxResult addOrder(@RequestBody Order order, @RequestHeader(name = "Authorization") String token ){
        Long userId = JwtUtil.getUserID(token);
        if (order == null){
            throw new RuntimeException("添加订单为空");
        }

        boolean flag = orderService.addOrder(order);
        if (flag){
            return AjaxResult.success().add("orderId",order.getId());
        }
        return AjaxResult.error();
    }

    @CheckPermission({"0","1"})
    @GetMapping("/selectOrderById")
    public AjaxResult selectOrderById(Integer id){
        if (id == null){
            throw new RuntimeException("查询订单id为空");
        }
        return AjaxResult.success().add("order",orderService.selectOrderById(id));
    }

    @CheckPermission({"0","1"})
    @PostMapping("/selectOrderByPage")
    public AjaxResult selectOrderByPage(@RequestBody Map<String,Object> params){
        return AjaxResult.success().add("pageInfo",orderService.selectOrderByPage(params));
    }
}
